package javase0829;

import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LoginServe {

    //登录请求
    public static final String LOGIN_ACTION = "login";

    //首页
    public static final String INDEX = "index";

    public static Map<String, String> aps = new HashMap<>();
    public static Map<String, String> tokens = new HashMap<>();

    public static List<ScDish> dishes = new ArrayList<>();

    /**
     * 账号为主键存储订单列表
     */
    public static Map<String,List<ScOrder>> orders = new HashMap<>();

    static {
        aps.put("foo", "123456");
        aps.put("goo", "qwerty");
        aps.put("hoo", "asdfgh");
        aps.put("ioo", "zxcvbn");

        dishes.add(new ScDish(1, "菜1", 10));
        dishes.add(new ScDish(2, "菜2", 14));
        dishes.add(new ScDish(3, "菜3", 12));
        dishes.add(new ScDish(4, "菜4", 20));
    }

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        while (true) {
            Socket socket = serverSocket.accept();
            ServeThread st = new ServeThread(socket);
            st.start();
            //sendAndReceive(socket);
        }

        //serverSocket.close();
    }
    public static void sendAndReceive(Socket socket) {
        try {
            InputStream is = socket.getInputStream();
            OutputStream oos = socket.getOutputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader bufferedReader = new BufferedReader(isr);
            String s = bufferedReader.readLine();
            System.out.println(s);
            String ss = s + "Serve";
            oos.write(ss.getBytes());
            oos.flush();
            oos.close();
            bufferedReader.close();
            isr.close();
            is.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static int id = 0;
    public static synchronized int initId() {
        id++;
        return id;
    }
}
